package com.cloud.ratelimiter;

import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: TongRui乀
 * @Date: 2020-02-25 11:36
 * @description：  自定义获取key
 */
public class CustomerKeyResolver implements KeyResolver {

    public static final String BEAN_NAME = "customKeyResolver";

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(getKey(exchange));
    }

    /**
     *
     * @param exchange
     * @return
     */
    private String getKey(ServerWebExchange exchange) {

        JSONObject data = new JSONObject();

        data.put("api", exchange.getRequest().getPath().toString());
        data.put("key", exchange.getRequest().getHeaders().get("token"));

        return data.toJSONString();
    }
}
